<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2018/10/1
 * Time: 下午3:31
 */
require_once('TCPDF/config/tcpdf_config.php');
require_once('TCPDF/tcpdf.php');

class Tcpdf_api extends TCPDF
{
    
    protected $orientation = 'P';
    protected $unit = 'mm';
    protected $format = 'A4';
    protected $unicode = true;
    protected $encoding = 'UTF-8';
    protected $diskcache = false;
    protected $pdfa = false;
    
    public function __construct($params = array())
    {
        //$orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8',
        // $diskcache = false, $pdfa = false;
        
        $this->initialize($params);
        
        
        parent::__construct($this->orientation, $this->unit, $this->format, $this->unicode, $this->encoding, $this->diskcache, $this->pdfa);
    }
    
    public function initialize($props = array())
    {
        // Convert array elements into class variables
        if (count($props) > 0)
        {
            foreach ($props as $key => $val)
            {
                if (property_exists($this, $key))
                {
                    $this->$key = $val;
                }
            }
        }
        
        return TRUE;
    }
    
    public function label($code, $num = 1)
    {
        $this->SetCreator(PDF_CREATOR);
        $this->SetAuthor('Nicola Asuni');
        $this->SetTitle('TCPDF Example 028');
        $this->SetSubject('TCPDF Tutorial');
        $this->SetKeywords('TCPDF, PDF, example, test, guide');
    
        $this->setPrintHeader(false);
        $this->setPrintFooter(false);
    
        $this->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
        $this->SetMargins(2, 2, 2);
    
        $this->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);
    
        $this->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
        $this->SetDisplayMode('fullpage', 'SinglePage', 'UseNone');
    
        $this->SetFont('times', 'B', 20);
    
        $style = array(
            'position' => 'C',
            'align' => 'C',
            'stretch' => false,
            'fitwidth' => true,
            'cellfitalign' => '',
            'border' => false,
            'hpadding' => 'auto',
            'vpadding' => 'auto',
            'fgcolor' => array(0,0,0),
            'bgcolor' => false, //array(255,255,255),
            'text' => true,
            'font' => 'helvetica',
            'fontsize' => 10,
            'stretchtext' => 6
        );
    
        $i = 1;
        while ($i <= $num) {
            $this->AddPage('L', array(60, 30));
            $this->write1DBarcode(strtoupper($code), 'C128A', '', '', '', '', 0.4, $style, 'N');
    
            $this->Ln();
            
            $i++;
        }
    
        $this->lastPage();
    
        $this->Output('example_028.pdf', 'I');
    }

    public function label_text($code, $num = 1)
    {
        $this->SetCreator(PDF_CREATOR);
        $this->SetAuthor('Nicola Asuni');
        $this->SetTitle('TCPDF Example 028');
        $this->SetSubject('TCPDF Tutorial');
        $this->SetKeywords('TCPDF, PDF, example, test, guide');

        $this->setPrintHeader(false);
        $this->setPrintFooter(false);

        $this->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        $this->SetMargins(2, 2, 2);

        $this->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);

        $this->setImageScale(PDF_IMAGE_SCALE_RATIO);

        $this->SetDisplayMode('fullpage', 'SinglePage', 'UseNone');

        $this->SetFont('helvetica', 'B', 35);

        $style = array(
            'position' => 'C',
            'align' => 'C',
            'stretch' => false,
            'fitwidth' => true,
            'cellfitalign' => '',
            'border' => false,
            'hpadding' => 'auto',
            'vpadding' => 'auto',
            'fgcolor' => array(0,0,0),
            'bgcolor' => false, //array(255,255,255),
            'text' => true,
            'font' => 'helvetica',
            'fontsize' => 12,
            'stretchtext' => 6
        );

        $i = 1;
        while ($i <= $num) {
            $this->AddPage('L', array(60, 30));
            $this->Write(25, $code, '', 0, 'C', true, 0, false, false, 0);

            $this->Ln();

            $i++;
        }

        $this->lastPage();

        $this->Output('example_028.pdf', 'I');
    }
}